//
//  HCRegisterSecondView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/2/14.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCRegisterSecondView.h"

#import "HCLogSecView.h"

#import "ZFActionSheet.h"

#import "WayDatePickerView.h"

#import "HCPopLoadingView.h"

@interface HCRegisterSecondView () <UITextFieldDelegate,ZFActionSheetDelegate> {
    HCLogSecView *username;
    HCLogSecView *pwd;
    HCLogSecView *address;
    HCLogSecView *phone;
    HCLogThrView *name;
    HCLogSecView *bir;
    HCLogSecView *idcard;
    HCLogThrView *chi_name;
    HCLogSecView *chi_bir;
    CGFloat height_keyboard;
    WayDatePickerView *pickerView;    //日期选择器

}

@end

@implementation HCRegisterSecondView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        //增加监听，当键盘出现或改变时收出消息
//        [[NSNotificationCenter defaultCenter] addObserver:self
//                                                 selector:@selector(keyboardDidShow:)
//                                                     name:UIKeyboardWillShowNotification
//                                                   object:nil];
        
        height_keyboard = 270;
        
        self.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
        
        //用户名
        username = [[HCLogSecView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, YBKAdaptiveH(45)) andImage:@"用户名" placeText:@"用户名"];
        username.promptTextField.delegate = self;
        username.promptTextField.tag = 1;
        [self addSubview:username];
        
        //密码
        pwd = [[HCLogSecView alloc] initWithFrame:CGRectMake(0, username.frame.origin.y+username.frame.size.height+1, frame.size.width, username.frame.size.height) andImage:@"密码" placeText:@"密码"];
        pwd.promptTextField.delegate = self;
        pwd.promptTextField.secureTextEntry = YES;
        pwd.promptTextField.tag = 2;
        [self addSubview:pwd];
        
        //地址
        address = [[HCLogSecView alloc] initWithFrame:CGRectMake(0, pwd.frame.origin.y+pwd.frame.size.height+1, frame.size.width, pwd.frame.size.height) andImage:@"地址" placeText:@"邮箱地址"];
        address.promptTextField.delegate = self;
        address.promptTextField.tag = 3;
        [self addSubview:address];
        
        //电话
        phone = [[HCLogSecView alloc] initWithFrame:CGRectMake(0, address.frame.origin.y+address.frame.size.height+1, frame.size.width, address.frame.size.height) andImage:@"电话" placeText:@"电话"];
        phone.promptTextField.delegate = self;
        phone.promptTextField.tag = 4;
        phone.promptTextField.text = [NSString stringWithFormat:@"%@",HCGETUSER(@"userphone")];
        phone.userInteractionEnabled = NO;
        [self addSubview:phone];
        
        //姓名——大人
//        name = [[HCLogThrView alloc] initWithFrame:CGRectMake(0, phone.frame.origin.y+phone.frame.size.height+8, frame.size.width, phone.frame.size.height) andImage:@"名称" placeText:@"姓名" images:@"性别" placetexts:@"性别"];
//        name.promptTextField.delegate = self;
//        name.promptTextField.tag = 5;
//        name.promptTextFields.delegate = self;
//        name.promptTextFields.tag = 6;
//        [self addSubview:name];
        
//        //生日
//        bir = [[HCLogSecView alloc] initWithFrame:CGRectMake(0, name.frame.origin.y+name.frame.size.height+1, frame.size.width, name.frame.size.height) andImage:@"生日" placeText:@"生日"];
//        bir.promptTextField.delegate = self;
//        bir.promptTextField.tag = 7;
//        [self addSubview:bir];
        
        //身份证号码
//        idcard = [[HCLogSecView alloc] initWithFrame:CGRectMake(0, bir.frame.origin.y+bir.frame.size.height+1, frame.size.width, bir.frame.size.height) andImage:@"身份证" placeText:@"身份证号"];
//        idcard.promptTextField.delegate = self;
//        idcard.promptTextField.tag = 8;
//        [self addSubview:idcard];
        
        //姓名——宝宝
        chi_name = [[HCLogThrView alloc] initWithFrame:CGRectMake(0, phone.frame.origin.y+phone.frame.size.height+8, frame.size.width, phone.frame.size.height) andImage:@"名称" placeText:@"宝宝姓名" images:@"性别" placetexts:@"宝宝性别"];
        chi_name.promptTextField.delegate = self;
        chi_name.promptTextField.tag = 9;
        chi_name.promptTextFields.delegate = self;
        chi_name.promptTextFields.tag = 10;
        [self addSubview:chi_name];
        
        //宝宝生日
        chi_bir = [[HCLogSecView alloc] initWithFrame:CGRectMake(0, chi_name.frame.origin.y+chi_name.frame.size.height+1, frame.size.width, chi_name.frame.size.height) andImage:@"生日" placeText:@"宝宝生日"];
        chi_bir.promptTextField.delegate = self;
        chi_bir.promptTextField.tag = 11;
        [self addSubview:chi_bir];
        
        UIImage *btn_image = [UIImage imageNamed:@"注册"];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((frame.size.width - btn_image.size.width)/2.0, frame.size.height - btn_image.size.height-15, btn_image.size.width, btn_image.size.height)];
        [btn setBackgroundImage:btn_image forState:UIControlStateNormal];
        [self addSubview:btn];
        [btn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 10) {
        [self endEditing:YES];
        ZFActionSheet *actionSheet = [ZFActionSheet actionSheetWithTitle:nil confirms:@[@"男",@"女"] cancel:@"取消" style:ZFActionSheetStyleCancel];
        actionSheet.delegate = self;
        actionSheet.tag =1;
        [actionSheet showInView:self.window];

        return NO;
    }else if (textField.tag == 11) {
        [self endEditing:YES];
        pickerView = [[WayDatePickerView alloc] init];
        pickerView.backgroundColor = [UIColor whiteColor];
        
        __block __weak WayDatePickerView *weak_picker = pickerView;
//        __block __weak HCRegisterSecondView *weakSelf = self;
        pickerView.result = ^(NSString *date){
            NSString *ss = [NSString stringWithFormat:@"%@",[PublicFunction getDateFromNSString:date]];
            NSLog(@"选中的日期:%@",ss);
            textField.text = ss;
            [weak_picker hidden];
        };
        [pickerView show];
        return NO;
    }else{
        return YES;
    }
}

-(void)clickAction:(ZFActionSheet *)actionSheet atIndex:(NSUInteger)index {
    if (actionSheet.tag == 1) {
        if (index == 0) {
            chi_name.promptTextFields.text = @"男";
        }else{
            chi_name.promptTextFields.text = @"女";
        }
    }
}

-(void)sureBtnAction:(UIButton *)btn {
    NSLog(@"提交注册");
    
//    if ([name.promptTextFields.text isEqualToString:@"男"]) {
//        name.promptTextFields.text = @"1";
//    }else if ([name.promptTextFields.text isEqualToString:@"女"]) {
//        name.promptTextFields.text = @"0";
//    } else {
//        [XHToast showBottomWithText:@"请正确填写性别"];
//    }
    
//    if ([chi_name.promptTextFields.text isEqualToString:@"男"]) {
//        chi_name.promptTextFields.text = @"1";
//    }else if ([chi_name.promptTextFields.text isEqualToString:@"女"]) {
//        chi_name.promptTextFields.text = @"0";
//    } else {
//        [XHToast showBottomWithText:@"请正确填写性别"];
//    }
    

//    if ((![PublicFunction isNull:username.promptTextField.text]) && (![PublicFunction isNull:pwd.promptTextField.text]) && (![PublicFunction isNull:address.promptTextField.text]) && (![PublicFunction isNull:phone.promptTextField.text]) &&  (![PublicFunction isNull:idcard.promptTextField.text]) && (![PublicFunction isNull:chi_name.promptTextField.text]) && (![PublicFunction isNull:chi_name.promptTextFields.text]) && (![PublicFunction isNull:chi_bir.promptTextField.text]) && [PublicFunction checkUserEmail:address.promptTextField.text]) {
        if ([self.delegate respondsToSelector:@selector(hc_registerData:password:address:phone:realName:sex:bir:idcard:chi_name:chi_sex:chi_bir:)] &&self.delegate) {
            NSString *ssa;
            if ([chi_name.promptTextFields.text isEqualToString:@"男"]) {
                ssa = @"1";
            }else{
                ssa = @"女";
            }
            [self.delegate hc_registerData:username.promptTextField.text password:pwd.promptTextField.text address:address.promptTextField.text phone:phone.promptTextField.text realName:name.promptTextField.text sex:name.promptTextFields.text bir:bir.promptTextField.text idcard:idcard.promptTextField.text chi_name:chi_name.promptTextField.text chi_sex:ssa chi_bir:chi_bir.promptTextField.text];
        }
//    }
    
}
//
//#pragma mark - notification handler
//
////当键盘出现或改变时调用
//- (void)keyboardDidShow:(NSNotification *)aNotification
//{
//    //获取键盘的高度
//    NSDictionary *userInfo = [aNotification userInfo];
//    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
//    CGRect keyboardRect = [aValue CGRectValue];
//    CGFloat height = keyboardRect.size.height;
//     = height;
//}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//    if (textField.tag>=3) {
//        [self animateTextField: textField up: YES distance:90];
//    }else if (textField.tag == 11) {
//    }
//    [self animateTextField: textField up: YES distance:height_keyboard];

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
//    [self animateTextField: textField up: NO distance:height_keyboard];
    if (textField.tag == 3) {
        if (![PublicFunction checkUserEmail:address.promptTextField.text]) {
            [XHToast showCenterWithText:@"邮箱格式错误" duration:4];
        }
    }
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up distance:(CGFloat)distance
{
    const int movementDistance = distance; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed
    
    int movement = (up ? -movementDistance : movementDistance);
    
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.frame = CGRectOffset(self.frame, 0, movement);
    [UIView commitAnimations];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
